package shujujiegou;

public class MySingleList {
    private Node head;
    private Node tail;
    private int length;

    private static class Node {
        int data;
        Node next;

        Node(int data) {
            this.data = data;
        }
    }

    public MySingleList() {
        System.out.println("---初始化链表---");
        this.head = null;
        this.tail = null;
        this.length =0;
        System.out.println("head=" + String.format("%12d", System.identityHashCode(head)));
    }
    public void addFirst(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            head = newNode;
            tail = newNode;
            newNode.next = head;
        } else {
            newNode.next = head;
            head = newNode;
            tail.next = head;
        }
        length++;
    }

    public void addLast(int data) {
        Node newNode = new Node(data);
        if (head == null) {
            addFirst(data);
            return;
        }
        tail.next = newNode;
        newNode.next = head;
        tail = newNode;
        length++;
    }

    public boolean remove(int data) {
        if (head == null) return false;

        Node current = head;
        Node prev = tail;
        boolean found = false;

        do {
            if (current.data == data) {
                found = true;
                break;
            }
            prev = current;
            current = current.next;
        } while (current != head);

        if (!found) return false;

        if (current == head && current == tail) {
            head = null;
            tail = null;
        } else if (current == head) {
            head = head.next;
            tail.next = head;
        } else if (current == tail) {
            prev.next = head;
            tail = prev;
        } else {
            prev.next = current.next;
        }

        length--;
        return true;
    }

    public void reverse() {
        if (head == null || head.next == head) return;

        Node prev = tail;
        Node current = head;
        Node next;

        do {
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        } while (current != head);

        head = prev;
        tail = current;
        tail.next = head;
    }

    public void print() {
        if (head == null) {
            System.out.println("链表为空");
            return;
        }

        Node current = head;
        StringBuilder sb = new StringBuilder();

        do {
            sb.append(current.data);
            if (current.next != head) {
                sb.append("，");
            }
            current = current.next;
        } while (current != head);

        System.out.println(sb.toString());
    }

    public static void main(String[] args) {
        MySingleList list = new MySingleList();

        System.out.println("--- 增加结点（头插法）---");
        list.addFirst(13);
        list.addFirst(14);
        list.addFirst(15);
        list.print();
        System.out.println("length: " + list.length);

        System.out.println("--- 增加结点（尾插法）---");
        list.addLast(21);
        list.addLast(22);
        list.addLast(23);
        list.print();
        System.out.println("length: " + list.length);

        System.out.println("---删除结点---");
        System.out.println("删除数据为5120的结点");
        System.out.println("删除前：");
        list.print();
        boolean result1 = list.remove(5120);
        System.out.println("---删除" + (result1 ? "成功" : "失败"));
        list.print();
        System.out.println("length: " + list.length);

        System.out.println("---删除数据为13的结点");
        System.out.println("删除前：");
        list.print();
        boolean result2 = list.remove(13);
        System.out.println("---删除" + (result2 ? "成功" : "失败"));
        list.print();
        System.out.println("length: " + list.length);

        System.out.println("---删除数据为23的结点");
        System.out.println("删除前：");
        list.print();
        boolean result3 = list.remove(23);
        System.out.println("---删除" + (result3 ? "成功" : "失败"));
        list.print();
        System.out.println("length: " + list.length);

        System.out.println("---反转链表---");
        System.out.println("反转前");
        list.print();
        list.reverse();
        System.out.println("---反转后");
        list.print();
    }
}